package com.example.demo.controller;

import com.example.demo.entity.TeamAssignment;
import com.example.demo.service.TeamAssignmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/api/team-assignments")
public class TeamAssignmentController {

    @Autowired
    private TeamAssignmentService teamAssignmentService;

    @PostMapping
    public ResponseEntity<TeamAssignment> createTeamAssignment(@RequestBody TeamAssignment teamAssignment) {
        TeamAssignment created = teamAssignmentService.createTeamAssignment(teamAssignment);
        return ResponseEntity.ok(created);
    }

    @GetMapping("/{id}")
    public ResponseEntity<TeamAssignment> getTeamAssignment(@PathVariable Long id) {
        TeamAssignment teamAssignment = teamAssignmentService.getTeamAssignmentById(id);
        return teamAssignment != null ? ResponseEntity.ok(teamAssignment) : ResponseEntity.notFound().build();
    }

    @GetMapping("/team/{teamId}")
    public ResponseEntity<List<TeamAssignment>> getTeamAssignmentsByTeam(@PathVariable Long teamId) {
        List<TeamAssignment> assignments = teamAssignmentService.getTeamAssignmentsByTeamId(teamId);
        return ResponseEntity.ok(assignments);
    }

    @GetMapping("/assignment/{assignmentId}")
    public ResponseEntity<List<TeamAssignment>> getTeamAssignmentsByAssignment(@PathVariable String assignmentId) {
        List<TeamAssignment> assignments = teamAssignmentService.getTeamAssignmentsByAssignmentId(assignmentId);
        return ResponseEntity.ok(assignments);
    }

    @GetMapping("/submitter/{submittedBy}")
    public ResponseEntity<List<TeamAssignment>> getTeamAssignmentsBySubmitter(@PathVariable Long submittedBy) {
        List<TeamAssignment> assignments = teamAssignmentService.getTeamAssignmentsBySubmittedBy(submittedBy);
        return ResponseEntity.ok(assignments);
    }

    @GetMapping
    public ResponseEntity<List<TeamAssignment>> getAllTeamAssignments() {
        List<TeamAssignment> assignments = teamAssignmentService.getAllTeamAssignments();
        return ResponseEntity.ok(assignments);
    }

    @PutMapping("/{id}")
    public ResponseEntity<TeamAssignment> updateTeamAssignment(@PathVariable Long id,
                                                               @RequestBody TeamAssignment teamAssignment) {
        teamAssignment.setId(id);
        TeamAssignment updated = teamAssignmentService.updateTeamAssignment(teamAssignment);
        return ResponseEntity.ok(updated);
    }

    @PatchMapping("/{id}/score")
    public ResponseEntity<Void> updateScore(@PathVariable Long id,
                                            @RequestParam BigDecimal score,
                                            @RequestParam Integer isExcellent) {
        boolean updated = teamAssignmentService.updateScoreAndExcellent(id, score, isExcellent);
        return updated ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }

    @PostMapping("/{id}/submit")
    public ResponseEntity<Void> submitAssignment(@PathVariable Long id,
                                                 @RequestParam String filePath) {
        boolean submitted = teamAssignmentService.submitAssignment(id, filePath);
        return submitted ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTeamAssignment(@PathVariable Long id) {
        boolean deleted = teamAssignmentService.deleteTeamAssignment(id);
        return deleted ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }

    @GetMapping("/excellent")
    public ResponseEntity<List<TeamAssignment>> getExcellentAssignments() {
        List<TeamAssignment> assignments = teamAssignmentService.getExcellentTeamAssignments();
        return ResponseEntity.ok(assignments);
    }

    @GetMapping("/team/{teamId}/assignment/{assignmentId}")
    public ResponseEntity<TeamAssignment> getByTeamAndAssignment(@PathVariable Long teamId,
                                                                 @PathVariable String assignmentId) {
        TeamAssignment assignment = teamAssignmentService.getByTeamIdAndAssignmentId(teamId, assignmentId);
        return assignment != null ? ResponseEntity.ok(assignment) : ResponseEntity.notFound().build();
    }

    @GetMapping("/team/{teamId}/count")
    public ResponseEntity<Integer> countByTeam(@PathVariable Long teamId) {
        int count = teamAssignmentService.countTeamAssignmentsByTeamId(teamId);
        return ResponseEntity.ok(count);
    }

    @GetMapping("/assignment/{assignmentId}/count")
    public ResponseEntity<Integer> countByAssignment(@PathVariable String assignmentId) {
        int count = teamAssignmentService.countTeamAssignmentsByAssignmentId(assignmentId);
        return ResponseEntity.ok(count);
    }
}